<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "{{%svn}}".
 *
 * @property int $id
 * @property int $patch_id
 * @property int $code
 * @property string $comment
 * @property string|null $detail
 * @property int $environment 环境 1 dev 2 dev2 3 rc 4 live
 * @property string|null $created_at
 * @property string|null $updated_at
 *
 * @property array|string|null $environmentList
 *
 * @property File[] $files
 * @property Patch $patch
 */
class Svn extends Base
{
    public $file;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%svn}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['patch_id'], 'required'],
            [['patch_id', 'code', 'environment'], 'integer'],
            [['detail'], 'string'],
            [['created_at', 'updated_at'], 'safe'],
            [['comment'], 'string', 'max' => 255],
            [
                ['patch_id'],
                'exist',
                'skipOnError' => true,
                'targetClass' => Patch::class,
                'targetAttribute' => ['patch_id' => 'id']
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'patch_id' => 'Patch ID',
            'code' => '版本号',
            'comment' => '简述',
            'detail' => '详述',
            'environment' => '开发环境',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * Gets query for [[Files]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getFiles()
    {
        return $this->hasMany(File::class, ['svn_id' => 'id']);
    }

    /**
     * Gets query for [[Patch]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getPatch()
    {
        return $this->hasOne(Patch::class, ['id' => 'patch_id']);
    }

    public function getEnvironmentList(?int $k = null)
    {
        $arr = [1 => 'DEV', 2 => 'DEV2', 3 => 'RC', 4 => 'LIVE'];
        return $k ? $arr[$k] : $arr;
    }
}
